package game.module.nn.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.entity.PlayingRole;
import game.module.hall.logic.QipaiConstants;
import game.module.hall.logic.QipaiDataManager;
import game.module.nn.bean.NnRoom;
import game.session.SessionManager;
import lion.common.ArrayUtility;
import lion.netty4.message.RequestMessage;
import lua.LuaMessageNn.PushNNBeginFight;

public class NnLogic {

	private static Logger logger = LoggerFactory.getLogger(NnLogic.class);

	static class SingletonHolder {
		static NnLogic instance = new NnLogic();
	}

	public static NnLogic getInstance() {
		return SingletonHolder.instance;
	}

	public NnRoom createNnRoom(int playerId, short roundCount) {
		String roomUuid = QipaiDataManager.getInstance().generateRoomUuid();
		NnRoom ddzRoom = new NnRoom();
		ddzRoom.setUuid(roomUuid);
		ddzRoom.setRoundCount(roundCount);
		List<Integer> playerList = new ArrayList<>();
		playerList.add(playerId);
		ddzRoom.setPlayerList(playerList);
		// put into cache
		NnDataManager.getInstance().addRoom(ddzRoom);
		QipaiDataManager.getInstance().addRoomUuid(roomUuid, QipaiConstants.ROOM_TYPE_NN);
		return ddzRoom;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			String randStr = RandomStringUtils.randomNumeric(6);
			logger.info(randStr);
		}
	}

	public void addRoomPlayer(int playerId, String room_id) {
		NnRoom ddzRoom = NnDataManager.getInstance().getNnRoom(room_id);
		if (ddzRoom == null) {
			return;
		}
		ddzRoom.getPlayerList().add(playerId);
	}

	public void lanNnGenerateCardsBack(RequestMessage msg) {
		logger.info("lan nn generate cards callback");
		PushNNBeginFight pushGameStart = new PushNNBeginFight();
		pushGameStart.room_server_host = HallServer.getInstance().getServerConfig().getRsSocketHost();
		pushGameStart.room_server_port = HallServer.getInstance().getServerConfig().getRsSocketPort();
		String roomUuid = msg.readString();
		int playerCount = msg.readInt();
		Map<Integer, List<Byte>> cardsMap = new HashMap<>();
		for (int i = 0; i < playerCount; i++) {
			int playerId = msg.readInt();
			int cardCount = msg.readInt();
			List<Byte> cards = new ArrayList<>();
			for (int j = 0; j < cardCount; j++) {
				byte aCard = msg.readByte();
				cards.add(aCard);
			}
			cardsMap.put(playerId, cards);
		}
		//
		for (Map.Entry<Integer, List<Byte>> aEntry : cardsMap.entrySet()) {
			int playerId = aEntry.getKey();
			List<Byte> cardsList = aEntry.getValue();
			PlayingRole pr = SessionManager.getInstance().getPlayer(playerId);
			if (pr != null && pr.isChannelActive()) {
				pushGameStart.my_cards = ArrayUtility.toByteArray(cardsList);
				pr.writeAndFlush(pushGameStart.build(pr.alloc()));
			}
		}
		// 清理缓存信息
		NnDataManager.getInstance().removeRoom(roomUuid);
		QipaiDataManager.getInstance().removeRoomUuid(roomUuid);
	}

}
